// pages/sign/sign.js
const utils = require("../../utils/util.js");
const globalData = getApp().globalData;

Page({

       /**
        * 页面的初始数据
        */
       data: {
              signPattern: '',                                                      // 当前签署模式(无控件签署: noControllerSign,有控件签署: hasControllerSign,审阅: check)
              controllers: [],                                                       // 控件组
              controllerDetailType: '',                                          // 控件详情类型
              blackBg: false,                                                      // 黑色背景开关
              controllerDetailOpen: false,                                     // 是否打开控件详情
              noteInfo: '发送短信',                                               // 短信验证码信息
              noteVerifyHidden: true,                                           // 短信验证弹窗隐藏
              imgUrl: 'http://file.test.cibfintech.com/',                   // 图片前缀url
              contractLists: [],                                                    // 合同列表
              sealLists: [],                                                          // 签章列表
              userMobile: '',                                                       // 用户手机号
              toController: '',                                                      // 移动至控件的高度
              onload: false,                                                        // 控件条加载完成
              contractDetail: {}                                                   // 合同详情
       },

       // 初始化值
       createControllerFlage: false,                                                    // 创建控件锁
       controllerAreaWidth: 0,                                                           // 合同区域的宽度  
       screenTop: 0,                                                                        // 当前视框距离顶部的距离
       dragControllerMoveFlag: false,                                                 // 拖拽控件移动锁
       currentController: {},                                                               // 当前控件
       customCanvas: wx.createCanvasContext('customSigner'),           // 自定义签章画板
       canvasFlag: false,                                                                   // 画板书写锁
       canvasLines: [],                                                                      // 画板历史路径
       noteFlag: true,                                                                       // 短信验证码发送锁
       noteVerify: '',                                                                         // 短信内容
       blackBgType: '',                                                                     // 黑色背景类型
       contractPages: 0,                                                                   // 合同总页数
       controllerAreaHeight: 0,                                                          // 合同区域的高度
       onePageHeight: 0,                                                                  // 单页合同的高度
       contractScreenHeight: 0,                                                         // 合同区域一屏的高度
       imgLoadTimeout: null,                                                             // 图片加载定时器
       endTriggerFlag: false,                                                             // 触发拖拽控件结束锁
       controllersPositions: {                                                             // 控件位置下标
              signatureController: {
                     index: 0,
                     controllers: []
              }
       },
       look: false,                                                                            // 是否为查看合同

       // 预备控件类型
       readyController: {},

       // 点击获取控件
       getController() {},

       // 点击获取控件(因为this指向问题，使用替代函数)
       getController_this(e) {
              this.blackBgType = 'controllerDetail';
              this.getController.call(this, e);
       },

       // 各签署模式下点击获取控件函数
       multipleGetController: {
              // 无控件签署
              'noControllerSign': function(e) {
                     // 确定预备控件类型
                     this.readyController = this.controllerStyle[e.currentTarget.dataset.type];
                     
                     // 打开创建控件锁，允许创建新控件
                     this.createControllerFlage = true;
              },

              // 有控件签署
              'hasControllerSign': function (e) {
                     let controllerType = this.controllersPositions[e.currentTarget.dataset.type];
                     let controllers = controllerType.controllers;

                     // 滚动到控件位置
                     this.setData({ toController: utils.toDecimals(controllers[controllerType.index++ % controllers.length].y) * this.controllerAreaHeight - 100 });
              },

              // 审阅
              'check': () => {}
       },

       // 控件样式
       controllerStyle: {},

       // 各签署模式下控件样式库
       multipleControllerStyle: {
              // 无控件签署
              'noControllerSign': {
                     signatureController: { type: 'signatureController', width: '10%', height: '10%', x: '0', y: '0', bg: '#4990e2' }                     // 签章控件
              },
              
              // 有控件签署
              'hasControllerSign': {
                     signatureController: { type: 'signatureController', width: '10%', height: '10%', x: '0', y: '0', bg: '#4990e2' }                     // 签章控件
              },

              // 审阅
              'check': {}
       },

       // 初始化合同区域尺寸
       initControllerAreaSize() {
              // 删除定时器
              clearTimeout(this.imgLoadTimeout);

              // 设置定时器
              this.imgLoadTimeout = setTimeout(() => {
                     let query = wx.createSelectorQuery();
                     query.select('#controllerArea').boundingClientRect()
                            .exec(res => {
                                   this.contractPages = this.data.contractLists.length;                                                           // 获取合同总页数
                                   this.onePageHeight = res[0].height / this.contractPages;                                                    // 获取单页合同高度
                                   this.controllerAreaWidth = res[0].width;                                                                            // 获取合同区域宽度
                                   this.controllerAreaHeight = res[0].height;                                                                          // 更新合同区域高度
                                   this.contractScreenHeight = wx.getSystemInfoSync().windowHeight - 55;                            // 获取合同区域一屏的高度
                            });
              }, 100);
       },

       // 图片加载完成
       imgLoad() {
              this.initControllerAreaSize();
       },

       // 触摸合同区域
       tapControllerArea(e) {
              // 检测创建控件锁
              if(!this.createControllerFlage) return;

              // 关闭创建控件锁
              this.createControllerFlage = false;

              // 创建新控件
              let newController = this.readyController;

              newController.x = utils.toPercent((e.detail.x - utils.toDecimals(newController.width) / 2 * this.controllerAreaWidth) / this.controllerAreaWidth);
              newController.y = utils.toPercent((e.detail.y + this.screenTop - utils.toDecimals(newController.height) / 2 * this.controllerAreaWidth) / this.controllerAreaHeight);

              // 添加新控件
              let newControllers = this.data.controllers;
              
              newControllers.push(newController);
              this.setData({ controllers: newControllers });

              // 边界校准
              e.currentTarget.dataset['index'] = newControllers.length - 1;
              setTimeout(() => {
                     this.dragControllerStart(e);
                     this.dragControllerEnd(e);
              }, 200);
       },

       // 计算当前视框距离顶部的距离
       getScreenTop(e) {
              this.screenTop = e.detail.scrollTop;
       },

       // 最大化计算当前视框距离顶部的距离
       maxScreenTop() {
              this.screenTop = this.controllerAreaHeight - this.contractScreenHeight;
       },

       // 拖拽控件开始
       dragControllerStart(e) {
              // 打开拖拽控件移动锁，允许拖拽控件移动
              this.dragControllerMoveFlag = this.data.signPattern === 'noControllerSign' ? true : false;

              // 创建当前控件模型
              let controller = {};
              let index = e.currentTarget.dataset.index;

              // 添加当前控件参数
              controller = this.data.controllers[index];
              controller.index = index;

              // 如果是移动事件则添加触摸点坐标
              controller.touchCoord = false;
              if (e.type === 'touchstart') {
                     controller.touchCoord = true;
                     controller.xTouch = e.changedTouches[0].clientX - utils.toDecimals(controller.x) * this.controllerAreaWidth;
                     controller.yTouch = e.changedTouches[0].clientY - utils.toDecimals(controller.y) * this.controllerAreaHeight + this.screenTop;
              }

              // 赋值当前控件
              this.currentController = {};
              this.currentController = controller;

              // 有控件签署模式下触发控件拖拽结束函数
              if (this.data.signPattern === 'hasControllerSign') {
                     this.endTriggerFlag = true;
                     this.dragControllerEnd(e);
                     this.endTriggerFlag = false;
              }
       },

       // 拖拽控件移动
       dragControllerMove(e) {
              if (!this.dragControllerMoveFlag) return;

              let controllers = this.data.controllers;
              let currentController = this.currentController;
              let controller = controllers[currentController.index];
              let xTouch, yTouch;

              // 是否有触点坐标
              if (currentController.touchCoord) {
                     xTouch = currentController.xTouch;
                     yTouch = currentController.yTouch;
              } else {
                     xTouch = utils.toDecimals(currentController.width) / 2 * this.controllerAreaWidth;
                     yTouch = utils.toDecimals(currentController.height) / 2 * this.controllerAreaWidth;
              }

              // 确定坐标
              controller.x = utils.toPercent((e.changedTouches[0].clientX - xTouch) / this.controllerAreaWidth);
              controller.y = utils.toPercent((e.changedTouches[0].clientY + this.screenTop - yTouch) / this.controllerAreaHeight);

              this.setData({ controllers: controllers });
       },

       // 拖拽控件结束
       dragControllerEnd(e) {
              // 拖拽控件结束锁
              if (this.data.signPattern === 'hasControllerSign' && !this.endTriggerFlag) return;

              // 关闭拖拽控件移动锁，禁止拖拽控件移动
              this.dragControllerMoveFlag = false;
              
              // 控件落地
              let controllers = this.data.controllers;
              let currentController = this.currentController;
              let controller = controllers[currentController.index];              
              let xTouch, yTouch;

              // 是否有触点坐标
              if (currentController.touchCoord) {
                     xTouch = currentController.xTouch;
                     yTouch = currentController.yTouch;
              } else {
                     xTouch = utils.toDecimals(currentController.width) / 2 * this.controllerAreaWidth;
                     yTouch = utils.toDecimals(currentController.height) / 2 * this.controllerAreaWidth;
              }

              // 确定坐标
              controller.x = (e.changedTouches[0].clientX - xTouch) / this.controllerAreaWidth;
              controller.y = (e.changedTouches[0].clientY + this.screenTop - yTouch) / this.controllerAreaHeight;

              // 边界校准
              // x轴校准
              if (controller.x < 0) controller.x = 0;
              if (controller.x > 1 - utils.toDecimals(controller.width)) controller.x = 1 - utils.toDecimals(controller.width);

              // y轴校准
              let controllerPage = Math.floor((this.controllerAreaHeight * controller.y) / this.onePageHeight);

              if (controller.y < 0) controller.y = 0;
              if ((this.controllerAreaHeight * controller.y) % this.onePageHeight > this.onePageHeight - 20 - utils.toDecimals(controller.height) * this.controllerAreaWidth) {
                     controller.y = (controllerPage * this.onePageHeight + this.onePageHeight - 20 - utils.toDecimals(controller.height) * this.controllerAreaWidth) / this.controllerAreaHeight;
              }

              // 添加控件参数
              controller.page = controllerPage + 1;
              controller.xPer = controller.x + utils.toDecimals(controller.width) / 2;
              controller.yPer = (this.controllerAreaHeight * controller.y) % this.onePageHeight / (this.onePageHeight - 20) + utils.toDecimals(controller.width) * 0.36;

              // 百分比坐标
              controller.x = utils.toPercent(controller.x);
              controller.y = utils.toPercent(controller.y);
              this.setData({ controllers: controllers });
       },

       // 触摸控件
       tapController() {
              this.openControllerDetail(this.currentController.type);
       },

       // 打开合同详情
       openContractDetail() {
              this.openControllerDetail('contractDetail');
       },

       // 打开控件详情
       openControllerDetail(type) {
              this.blackBgType = 'controllerDetail';
              this.setData({
                     controllerDetailType: type,
                     blackBg: true,
                     controllerDetailOpen: true
              });
       },

       // 关闭控件详情
       closeControllerDetail() {
              this.setData({
                     controllerDetailType: '',
                     controllerDetailOpen: false
              });
              setTimeout(() =>  this.setData({ blackBg: false }), 150);
       },

       // 关闭黑色背景
       closeBlackBg() {
              // 关闭控件详情
              if (this.blackBgType === 'controllerDetail') this.closeControllerDetail();

              // 关闭短信验证
              if (this.blackBgType === 'noteVerify') this.closeNoteVerify();
       },

       // 选择控件图片
       selectImg(e) {
              let controllers = this.data.controllers;
              let currentController = this.currentController;
              let controller = controllers[currentController.index];

              controller.bg = `url(${this.data.imgUrl + e.currentTarget.dataset.url}) no-repeat center center / 100%;`;
              controller.content = e.currentTarget.dataset.url;
              this.setData({ controllers: controllers });

              // 关闭控件详情
              this.closeControllerDetail();
       },

       // 删除控件
       deleteController() {
              let controllers = this.data.controllers;
              let currentController = this.currentController;
              delete controllers[currentController.index];

              this.setData({ controllers: controllers });

              // 关闭控件详情
              this.closeControllerDetail();    
       },

       // 自定义签章
       customSigner() {
              this.openControllerDetail('customSinger');
       },
       
       // 初始化自定义签章
       initCustom() {
              this.customCanvas.setStrokeStyle("#000000");                    // 画笔颜色
              this.customCanvas.setLineWidth(1);                                    // 画笔粗细
       },

       // 画板触摸开始
       canvasStart(e) {
              this.canvasFlag = true;
              this.storageLines('move', e.changedTouches[0].x, e.changedTouches[0].y);
       },

       // 画板触摸移动
       canvasMove(e) {
              if (!this.canvasFlag) return;
              this.storageLines('line', e.changedTouches[0].x, e.changedTouches[0].y);
              this.draw();
       },

       // 画板触摸结束
       canvasEnd() {
              this.canvasFlag = false;
       },

       // 存储书写路径
       storageLines(type, x, y) {
              this.canvasLines.push({ type: type, x: x, y: y });
       },

       // 画路径
       draw() {
              this.canvasLines.map((res) => {
                     if (res.type === 'move') this.customCanvas.moveTo(res.x, res.y);
                     if (res.type === 'line') this.customCanvas.lineTo(res.x, res.y);
              });
              this.customCanvas.stroke();
              this.customCanvas.draw();
       },

       // 生成签章
       createSigner() {
              // 自定义签章不能为空
              if (this.canvasLines.length === 0) {
                     return wx.showModal({
                            content: '自定义签章不能为空',
                            showCancel: false
                     });
              }

              let _this = this;

              // 生成签章文件
              wx.canvasToTempFilePath({
                     canvasId: 'customSigner',
                     success: res => {
                            // loading
                            wx.showToast({ title: 'loading...', icon: 'loading', duration: 1500, mask: true });

                            // 提交签章
                            _this.submitSeal(res.tempFilePath, () => {
                                   // 关闭状态框
                                   wx.showToast({ title: 'loading...', icon: 'loading', duration: 1 });

                                   // 关闭控件详情
                                   _this.closeControllerDetail();

                                   // 清空画板
                                   _this.cleanCanvas();
                            });
                     }
              })
       },

       // 提交签章
       submitSeal(tempFilePath, calback) {
              let _this = this;
              wx.uploadFile({
                     url: `${globalData.http}file/${globalData.nodeNodeId }/${globalData.token}`,
                     filePath: tempFilePath,
                     name: 'file',
                     success: res => {
                            // 更改签章图片
                            let e = {currentTarget: {dataset: {url: res.sealUri }}};

                            _this.selectImg(e);

                            // 插入手写签章
                            let sealLists = this.data.sealLists;

                            sealLists.unshift({ sealUri: res.sealUri });
                            this.setData({ sealLists: sealLists });

                            // 关闭控件详情、清空画板
                            calback();
                     }
              });
       },

       // 清除画板
       cleanCanvas() {
              this.canvasLines = [];
              this.customCanvas.draw();
       },

       // 发送验证短信
       sendNote() {
              if (!this.noteFlag) return;
              this.noteFlag = false;

              // 倒计时
              this.countdown();

              wx.request({
                     url: `${globalData.http}phone/${globalData.pathInfo.query.mark}/${globalData.token}`,
                     method: 'GET',
                     data: { mobile: globalData.mobile },
                     success: res => {

                     }
              });
       },

       // 倒计时
       countdown() {
              let time = 59;
              this.setData({ noteInfo: `${time}s` });
              let noteInt = setInterval(() => {
                     if (time === 0) {
                            clearInterval(noteInt);
                            this.setData({ noteInfo: '发送短信' });
                            return this.noteFlag = true;
                     }
                     this.setData({ noteInfo: `${--time}s` });
              }, 1000);
       },

       // 接受输入的验证码
       changeNote(value) {
              this.noteVerify = value.detail.value;
       },

       // 提交合同
       submitContract() {
              this.multipleSubmitLibs[this.data.signPattern].call(this);
       },

       // 不同签署模式下提交函数
       multipleSubmitLibs: {
              // 无控件签署
              noControllerSign: function () {
                     // loading
                     wx.showToast({ title: 'loading...', icon: 'loading', duration: 15000, mask: true });

                     let _this = this;

                     wx.request({
                            url: `${globalData.http}contract/${globalData.nodeNodeId}/${globalData.token}`,
                            method: 'POST',
                            data: {
                                   actionType: "SIGN",
                                   fieldVOS: _this.getControllersInfo(),
                                   verifyCode: _this.noteVerify
                            },
                            success: res => {
                                   // 关闭状态框
                                   wx.showToast({ title: 'loading...', icon: 'loading', duration: 1 });

                                   if (res.statusCode === 500) return wx.showModal({ content: res.data.meta.msgs[0], showCancel: false });
                                   _this.closeBlackBg();
                                   wx.showModal({content: '合同签署成功！', showCancel: false, success: () => {
                                                 wx.navigateTo({ url: '/pages/end/end' });
                                   }});
                            }
                     });
              },
              // 有控件签署
              hasControllerSign: function () {
                     // loading
                     wx.showToast({ title: 'loading...', icon: 'loading', duration: 15000, mask: true });

                     let _this = this;

                     wx.request({
                            url: `${globalData.http}contract/${globalData.nodeNodeId}/${globalData.token}`,
                            method: 'POST',
                            data: {
                                   actionType: "SIGN",
                                   fieldVOS: _this.getControllersInfo(),
                                   verifyCode: _this.noteVerify
                            },
                            success: res => {
                                   // 关闭状态框
                                   wx.showToast({ title: 'loading...', icon: 'loading', duration: 1 });

                                   if (res.statusCode === 500) return wx.showModal({ content: res.data.meta.msgs[0], showCancel: false });
                                   _this.closeBlackBg();
                                   wx.showModal({ content: '合同签署成功！', showCancel: false, success: () => {
                                          wx.navigateTo({ url: '/pages/end/end' });
                                   }});
                            }
                     });
              },
              // 审阅
              check: function() {
                     // loading
                     wx.showToast({ title: 'loading...', icon: 'loading', duration: 15000, mask: true });

                     let _this = this;

                     wx.request({
                            url: `${globalData.http}contract/${globalData.nodeId}/${globalData.token}`,
                            method: 'PUT',
                            success: res => {
                                   // 关闭状态框
                                   wx.showToast({ title: 'loading...', icon: 'loading', duration: 1 });

                                   if (res.statusCode === 500) return wx.showModal({ content: res.data.meta.msgs[0], showCancel: false });
                                   _this.closeBlackBg();
                                   wx.showModal({ content: '合同审阅成功！', showCancel: false , success: () => {
                                          wx.navigateTo({ url: '/pages/end/end' });
                                   }});
                            }
                     });
              }
       },

       // 获取控件信息
       getControllersInfo() {
              let controllersInfo = [];

              this.data.controllers.map(res => {
                     let controller = {
                            content: res.content,
                            widthPer: utils.toDecimals(res.width),
                            heightPer: utils.toDecimals(res.height),
                            pageNum: res.page,
                            xPer: res.xPer,
                            yPer: res.yPer,
                            type: this.typeChangeLibs[res.type]
                     };
                     controllersInfo.push(controller);
              });

              return controllersInfo;
       },

       // 类型转换库
       typeChangeLibs: {
              signatureController: 'SIGNATURE'
       },

       // 类型转换库2
       typeChangeLibs2: {
              SIGNATURE: 'signatureController'
       },

       // 打开短信验证弹窗
       openNoteVerify() {
              if (this.data.signPattern === 'check') return this.multipleSubmitLibs[this.data.signPattern].call(this);

              // 验证控件是否全部填写
              if (!this.checkControllerIntegrity()) return;

              // 打开短信验证弹窗
              this.blackBgType = 'noteVerify';
              this.setData({ 
                     noteVerifyHidden: false,
                     blackBg: true
              });
       },

       // 验证控件是否全部填写
       checkControllerIntegrity() {
              let integrity = true;

              // 是否存证控件检测
              if (this.data.controllers.length === 0) {
                     wx.showModal({ content: '控件不能为空', showCancel: false });
                     integrity = false;
              }

              // 控件信息完整检测
              this.data.controllers.map(res => {
                     if (!res.content) {
                            wx.showModal({ content: '控件信息填写不完整', showCancel: false });
                            integrity = false;
                     }
              });
              return integrity;
       },

       // 关闭短信验证弹窗
       closeNoteVerify() {
              this.setData({
                     noteVerifyHidden: true,
                     blackBg: false
              });
       },

       // 初始化合同
       initContract() {
            console.log(globalData.pathInfo);
              // 如果路由中mark值为null，则显示默认的示例合同
              let mark = globalData.pathInfo.mark === 'null' ? '1988964062318315527' : globalData.pathInfo.mark;

              wx.request({
                     url: `${globalData.http}contract/${mark}/${globalData.code }`,
                     method: 'GET',
                     success: res => {
                            // 加载合同图片
                            this.setData({ contractLists: res.data.contract.contractPictureVOList }, () => {
                                   // 初始化合同详情
                                   let nodeVOList = res.data.nodeVOList;
                                   let newNodeVOList = [];

                                   nodeVOList.map(res => {
                                          let list = {
                                                 actionType: utils.toActionType(res.actionType),
                                                 level: res.level,
                                                 name: res.userVO.name,
                                                 mobile: res.userVO.mobile,
                                                 email: res.userVO.email
                                          };

                                          newNodeVOList.push(list);
                                   });
                                   this.setData({ contractDetail: {
                                          title: res.data.title,
                                          expirationDate: utils.toDate(res.data.expirationDate),
                                          remark: res.data.remark,
                                          senderName: res.data.sender.name,
                                          nodeVOList: newNodeVOList
                                   } });

                                   // 更改标题
                                   wx.setNavigationBarTitle({ title: res.data.title });

                                   // 判断是否为查看合同
                                   if (this.look) return;

                                   // 初始化合同区域尺寸
                                   this.initControllerAreaSize();
                                   
                                   // 判断签署模式
                                   this.judgeSignPattern(res.data.contract.contractPictureVOList, res.data.currentNode[0].actionType);

                            });

                            // 判断是否为查看合同
                            if (this.look) return;

                            // 存储nodeId和nodeNodeId
                            if (res.data.currentNode.length === 0) return wx.showModal({ content: '当前合同仅能查看', showCancel: false });
                            globalData.nodeNodeId = res.data.currentNode[0].nodeId;
                            globalData.nodeId = res.data.currentNode[0].id;
                     },
                     fail: err => {
                            wx.showModal({ content: err.errMsg, showCancel: false });
                     }
              });
       },

       // 获取签章图片
       getSeals() {
              wx.request({
                     url: `${globalData.http}node/${globalData.nodeNodeId }/seal/${globalData.token }`,
                     method: 'GET',
                     success: res => {
                            this.setData({ sealLists: res.data.sealVOList });
                     }
              });
       },

       // 判断签署模式
       judgeSignPattern(contractsList, actionType) {
              // 如果actionType为1，则为审阅模式
              if (actionType === 1) {
                     this.setData({ signPattern: 'check', onload: true });

              // 如果actionType不为0，则进一步判断签署模式
              } else {
                     // 请求合同控件
                     this.requestController(contractsList);

                     // 获取签章图片
                     this.getSeals();
              }
       },

       // 请求合同控件
       requestController(contractsList) {
              wx.request({
                     url: `${globalData.http}node/${globalData.nodeNodeId}/${globalData.token}`,
                     method: 'GET',
                     data: {
                            contractPageSize: contractsList.length
                     },
                     success: res => {
                            // 判断签署模式
                            let pattern = 'noControllerSign';
                            res.data.fields.map(res => {
                                   if (res.fieldVOS) pattern = 'hasControllerSign';
                            });
                            this.setData({ signPattern: pattern, onload: true });

                            // 初始化控件样式
                            this.controllerStyle = this.multipleControllerStyle[this.data.signPattern];

                            // 初始化点击获取控件
                            this.getController = this.multipleGetController[this.data.signPattern];

                            if (pattern !== 'hasControllerSign') return;
                            // 添加控件
                            setTimeout(() => {
                                   this.addController(res);

                                   // 初始化控件位置下标
                                   this.data.controllers.map(res => this.controllersPositions[res.type].controllers.push(res));
                            }, 100);
                     }
              });
       },

       // 添加控件
       addController(fieldsList) {

              let controllers = [];

              fieldsList.data.fields.map(res => {
                     if (res.fieldVOS) {
                            res.fieldVOS.map(res => {
                                   let controllerTem = this.controllerStyle[this.typeChangeLibs2[res.type]];
                                   let controller = {
                                          bg: controllerTem.bg,
                                          height: controllerTem.height,
                                          width: controllerTem.width,
                                          type: controllerTem.type,
                                          x: utils.toPercent(res.xPer),
                                          y: utils.toPercent(1 / this.contractPages * (res.pageNum - 1) + (res.yPer * (this.onePageHeight - 10) / this.onePageHeight) / this.contractPages)
                                   };

                                   controllers.push(controller);
                            });
                     }
              });

              this.setData({ controllers: controllers });
       },

       /**
        * 生命周期函数--监听页面初次渲染完成
        */
       onReady: function () {
              // 初始化合同
              this.initContract();

              // 初始化自定义签章
              this.initCustom();

              // 初始化用户信息
              this.setData({ userMobile: globalData.mobile });
       },

       onLoad: function (option) {
              // 如果url有look参数，说明是查看合同
              if (option.look) this.look = true;
       }

})